<?
		session_start();
	    include_once 'functionsCompanies.php';
		include_once 'functions_db.php';
		include 'headermenu.php';


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <link rel="stylesheet" href="yui.css" type="text/css">
<head>
	<title>Methodology Portal</title>
</head>
<body>
<?php   
     //modificare: prin if-ul de mai jos se verifica daca s-a realizat logarea; deasemenea acesta trebuie sa apara in fiecare pagina
	if(ISSET($_SESSION["userId"]) == FALSE)
    {
         echo "<script type =\"text/javascript\">window.location.href=\"methodology.php\";</script>";
		 
		 exit();
	}  
?>

<div id="dhtmltooltip"></div>
<script type="text/javascript">
/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip
</script>

<div id="doc" class="yui-t7">
   
   <div id="bd" role="main">
   
   <div class="yui-gc">
    <div class="yui-u first">
	<!-- YOUR DATA GOES HERE -->
    <form action="" name="testQR" method="post" >
	<div align="right">  <input type="submit" name="add" value="Add company" align="right"> </div>
	</form>
	    </div>
    <div class="yui-u">
	<!-- YOUR DATA GOES HERE -->
	    </div>
 </div>
   
	<div class="yui-gc">
		<div class="yui-u first">
			<!-- YOUR DATA GOES HERE -->


	<?
	$dbh=dbConn();
	//####FUNCTII:
	//####se pare ca daca pun functiile inainte se da refresh la pagina singura!!! si cred ca stiu de ce!
	//####STERGE PROIECT:
	if(isset($_POST['qr_del']))
	{
		//echo ("Proiectul identificat cu numarul ".$_GET[rowid]." a fost sters.");
		mysql_query("delete from companies where id = ".$_GET[rowid].";") or die(mysql_error());
			unset($_GET[rowid]);
		//refreshPage('http://localhost/methodology/');
	}

	//####ADAUGA PROIECT:
	if(isset($_POST['qr_add']))
	{
		if($_POST['cname'] != "")
		{
			//echo ("A fost adaugat un nou proiect.");		
			//echo $_FILES['userFile']['name'];
			
			if($_FILES['userFile']['size'] > 0)
			{
				$fileName = $_FILES['userFile']['name'];
			
				copy($_FILES['userFile']['tmp_name'], "companies_pics/".$fileName);	
				$filePath = "companies_pics/".$fileName;
			}
			
			
			mysql_query("INSERT INTO companies (
			id,account, name, cui, j, street, nr, building, floor, city, county, country, postal_code, bank, bank_account, picture) 
			VALUES (NULL,'".$_SESSION['account']."','".$_POST['cname']."', '".$_POST['cui']."', '".$_POST['j']."', '".$_POST['street']."', '".$_POST['nr']."', 
			'".$_POST['building']."', '".$_POST['floor']."', '".$_POST['city']."', '".$_POST['county']."', '".$_POST['country']."', 
			'".$_POST['postalcode']."', '".$_POST['bank']."', '".$_POST['bank_account']."', '$filePath');") or die(mysql_error());  
			
			unset($_GET[rowid]);
			
			//refreshPage('http://localhost/methodology/');
		}
	}

	//####MODIFICA PROIECT:
	if(isset($_POST['qr_update']))
	{
			//echo $_FILES['userFile']['name'];
			if($_FILES['userFile']['size'] > 0)
			{
				$fileName = $_FILES['userFile']['name'];
				//echo "filename: ".$fileName;
				copy($_FILES['userFile']['tmp_name'], "companies_pics/".$fileName);	
				$filePath = "companies_pics/".$fileName;
			
				$sql = "UPDATE `companies` SET `name` = '".$_POST['cname']."', `cui` = '".$_POST['cui']."', `j` = '".$_POST['j']."', `street` = '".$_POST['street']."', `nr` = '".$_POST['nr']."', `building` = '".$_POST['building']."', `floor` = '".$_POST['floor']."', `city` = '".$_POST['city']."', `county` = '".$_POST['county']."', `country` = '".$_POST['country']."', `postal_code` = '".$_POST['postalcode']."', `bank` ='".$_POST['bank']."', `bank_account` = '".$_POST['bank_account']."',`picture` =  '$filePath' WHERE `companies`.`id` = ".$_GET[rowid].";";
			}
			else
			{
				$sql = "UPDATE `companies` SET `name` = '".$_POST['cname']."', `cui` = '".$_POST['cui']."', `j` = '".$_POST['j']."', `street` = '".$_POST['street']."', `nr` = '".$_POST['nr']."', `building` = '".$_POST['building']."', `floor` = '".$_POST['floor']."', `city` = '".$_POST['city']."', `county` = '".$_POST['county']."', `country` = '".$_POST['country']."', `postal_code` = '".$_POST['postalcode']."', `bank` ='".$_POST['bank']."', `bank_account` = '".$_POST['bank_account']."' WHERE `companies`.`id` = ".$_GET[rowid].";";
			}
		//echo "filePath: ".$filePath."/n";
		//echo $_FILES['userFile']['name'];
		//echo ("Proiectul identificat cu numarul ".$_GET[rowid]." a fost modificat.");
		
		
		mysql_query($sql) or die(mysql_error());  
			unset($_GET[rowid]);
		//refreshPage('http://localhost/methodology/');
	}




	$result = returnQuery("Select * from companies where account=".$_SESSION["account"].";",$dbh);
	//####afisam capu' la tabel:
	
	//####afisam inregistrarile in tabela:
	echo "<table id=\"table_companies\" width='100%' style=\"vertical-align: top;\">";
	echo "<tbody valign='top'>";
	$i = 0;
	while ($row = mysql_fetch_assoc($result)) {
		if ($i % 2 == 0)
			{
				echo "<tr>";
			}
		echo "<td>";
		echo "<div class='yui-g'>";
		//echo "<br>";
		echo "<table border='1' width='390px' vertical-align:'top'>";
		
		echo "<tr><td colspan='2' bgcolor=#00AFF2> <a onMouseover=\"ddrivetip('Click on the company name to edit details')\";
 onMouseout=\"hideddrivetip()\">Name: </a><a href='companies.php?rowid=".$row["id"]."'>".$row["name"]." </a> </td><td bgcolor=#00AFF2>
				Logo</td></tr></td>";
		echo "<tr><td width='250'>Cui: ".$row["cui"]."</td><td width='250'>J: ".$row["j"]."</td><td rowspan='4'>";
			//verificare daca are sigla:
			if ($row['picture']==null) {
				$row['picture'] = "companies_pics/NoPhotoAvailable.jpg";
			}
		echo "<img src='".$row['picture']."' width='50' height='50' /> </td></tr>";
				
		echo "<tr><td colspan='2'>Address: ".$row["street"]." ".$row["nr"]." ".$row["building"]." ".$row["floor"]." ".$row["city"]." ".$row["county"]." ".$row["country"]." ".$row["postal_code"]."</td></tr>";
		echo "<tr><td width='250'>Bank: ".$row["bank"]."</td><td width='250'>Account: ".$row["bank_account"]."</td></tr>";
  
        echo "</table>";
		//echo "<br>";
		echo "</div>";
		echo "</td>";
		if ($i % 2 == 1)
			{
				echo "</tr>";
			}		
		$i = $i + 1;
		
	}
	echo "</tbody>";
	echo "</table>";
	mysql_free_result($result);	
	?>
 

 
		</div>
    <div class="yui-u">
 <!-- AL DOILEA ECRAN!!! -->
 	<?php

	
	
		//####afisam sectiunea add/update/delete:
	if(isset($_POST['add'])) 
	{
		unset($_GET[rowid]);
		display($_GET[0]);
	}
	
	//####Functie pentru selectarea unui proiect:
	if (isset($_GET[rowid])!=0)
	{
		display($_GET[rowid]);
	}
	
	
	//####Functie pt butonul Anulare:
	if(isset($_POST['qr_cancel'])) 
	{
		unset($_GET[rowid]);
		
		refreshPage('http://localhost/methodology/companies.php');
	}
	
	//####Inchidem sesiunea DB:
	dbClose($dbh);
?>

 <br>

		
     </div>
</div>
 

	</div>
   
</div>
</body>
</html>
<? include 'footer.php'; ?>